package com.tueye.web.controller.usermanager;


import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.tueye.entity.Topics;
import com.tueye.entity.user.User;
import com.tueye.entity.user.UserCollectionInfo;
import com.tueye.service.ITopicService;
import com.tueye.service.usermanager.IUserTopicManager;
import com.tueye.utils.DateUtils;
import com.tueye.utils.SystemContext;
import com.tueye.utils.TuEyeConstant;
import com.tueye.utils.UUIDGenerator;
import com.tueye.utils.servlet.CaptchaServiceSingleton;
import com.tueye.web.controller.BaseController;
import com.tueye.web.controller.blog.PagerModel;

@SuppressWarnings("unchecked")
@Controller
public class UserTopicManagerController extends BaseController {
	private Log log = LogFactory.getLog(UserTopicManagerController.class);
	
	@Autowired
	@Qualifier("userTopicManger")
	IUserTopicManager userTopicManger;
	@Autowired
	@Qualifier("topicService")
	ITopicService topicService;
 
	@RequestMapping("/usertopicmanager.do")
	public String topic (HttpServletRequest request, HttpServletResponse response) throws Exception {
		User user =  (User) request.getSession().getAttribute(TuEyeConstant.TUEYE_USER);
        if(user  == null){
        	 return "pages/topics/usermanager/userlogin";  
        }
		 String target ="/pages/topics/usermanager/collecttopic";
		 String hql = "select t.topics from UserCollectionInfo t where t.userTopicType = 1 and t.user.id='"+user.getId() +"'";
		 PagerModel pm =  userTopicManger.getAllTopicByUserId(hql, SystemContext.getOffset(), SystemContext.getPagesize());
		 request.setAttribute("pm", pm);
		 return target; 
		
	}
	
	//用户收藏
	@RequestMapping("/shoucang.do") 
	public String updateUserinfo (HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		String topicid = request.getParameter("topicid");
		System.out.println("!!!!!!!!!!!!!!!!!!!!!!!!!!!"+topicid);
		User user =  (User) request.getSession().getAttribute(TuEyeConstant.TUEYE_USER);
		int userTopicType = 2;
		String userId = user.getId();	
		List list = userTopicManger.getOneTopicByAll(topicid,userId);
		if(list.size()==0){   
			  Topics topic = topicService.getTopicById(topicid);
			  UserCollectionInfo ui = new UserCollectionInfo();
			  String id = UUIDGenerator.getUUID();
			  ui.setUser(user);
			  ui.setTopics(topic);
			  ui.setId(id);
			  ui.setSys_time(DateUtils.getCurrentDate());
			  ui.setUserTopicType(userTopicType);
			  userTopicManger.saveShouCang(ui);
			  response.getWriter().write("true");
			  System.out.println("收藏成功!");
			  }else{   
     	       response.getWriter().write("true");
     	       System.out.println("该收藏以存在!");
			  }   
		return null; 
		
	}
	
	//用户退出登录
	@RequestMapping("/userexit.do")
	public String userexit (HttpServletRequest request, HttpServletResponse response) throws Exception {
		User user =  (User) request.getSession().getAttribute(TuEyeConstant.TUEYE_USER);
		String target = "pages/topics/usermanager/userlogin";  
        if(user  == null){
        	 return target;  
        }else{
        	request.getSession().removeAttribute(TuEyeConstant.TUEYE_USER);
        	 return target;  
        } 
		
	}
	 

}
